<!-----------通过绑定手机修改密码页面------------->
<template>
  <div>
    <topheader :toptitle="toptitle"/>
    <div class="wrapper findbackpwd-byPhone">
      <div class="wrapper-inner">
        <img class="xy-logo" src="static/img/xy-logo.png"/>
        <div class="go-login" id="goLogin">
          <p class="pwdtip">您正在进行密码修改</p>
          <div class="send-code">已向您的手机：<span>{{mobile}}</span> 发送验证码</div>
          <div class="login-item yk-code">
            <input type="text" v-model:value="uservalidatecode" placeholder="请输入验证码">
            <span class="v-code" @click="validateCode">{{validateMsg}}</span>
          </div>
          <div class="xy-pwd-new">
            <input id="bindusernewPwd" name="bindusernewpwd" type="password" v-model:value="newpwd"
                   placeholder="请输入新密码，6-12字符"/>
          </div>
          <div class="xy-pwd-sure">
            <input id="bindusernewPwd2" name="bindusernewpwd2" type="password" v-model:value="surenewpwd"
                   placeholder="请确认密码"/>
          </div>
          <div class="btn btn-normalsize btn-bluebg btn-surefindback" @click="surefindBack">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Vue from "vue"
  import topheader from "@/components/topheader"
  import Message from "iview/src/components/message"
  import apiConfig from "../config/api.js"
  import md5 from "js-md5"

  Vue.prototype.$Message = Message
  export default {
    name: "changepwd",
    components: {
      "topheader": topheader
    },
    data() {
      return {
        toptitle: "修改密码",
        mobile: "",
        validateMsg: "重新发送",
        isValidateing: false,
        uservalidatecode: "",
        newpwd: "",
        surenewpwd: ""
      }
    },
    created() {
      this.mobile = this.$route.params.mobile
      if (this.mobile !== "") {
      	//倒计时60s
	      var _this = this
	      this.isValidateing = true
	      var endTime = new Date().getTime() + (60 * 1000) + 50;
	      var t = 0;
	      var timer = setInterval(function () {
	        t = endTime - new Date().getTime();
	        if (t > 0) {
	          _this.validateMsg = "倒计时" + Math.floor(t / 1000 % 60) + "s"
	        } else {
	          clearInterval(timer);
	          _this.validateMsg = "重新发送"
	          _this.isValidateing = false
	        }
	      }, 1000);
      }
      
    },
    methods: {
      surefindBack: function () {
        if (this.uservalidatecode === "") {
          this.$Message.warning("请输入验证码")
          return
        } else if (this.newpwd === "") {
          this.$Message.warning("请输入新密码")
          return
        } else if (this.newpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.newpwd)) {
          this.$Message.warning("密码格式错误，请输入6-12字符密码")
          return
        } else if (this.surenewpwd === "") {
          this.$Message.warning("请确认密码")
          return
        } else if (this.surenewpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.surenewpwd)) {
          this.$Message.warning("请确认新密码")
          return
        } else if (this.newpwd !== this.surenewpwd) {
          this.$Message.warning("两次输入的密码不一致")
          return
        } else {
					// 请求参数
          let params = {
            appid: userInfoKit.appid,
            token: userInfoKit.getUserInfo().token,
            code: this.uservalidatecode,
            newPassword: md5(this.newpwd),
            repeatPassword: md5(this.surenewpwd)
          }					
					// 请求通过绑定的手机号修改密码接口
	        this.$http({
	              method: "GET",
	              url: apiConfig.app.user.modifyPwdByMobile,
	              params: params,
	              emulateJSON: true
            }).then(function (resp) {
	            if (resp.body.code == "200") {
                //提交成功后跳转到密码修改成功页面
                this.$router.push('/changepwdsuccess');
	            } else {
	              this.$Message.warning(resp.body.msg)
	            }
	        })				
        }
      },
       validateCode: function () {
        if (this.isValidateing) {
          return false
        }
        var _this = this
        // 请求参数
        let params = {
          appid: userInfoKit.appid,
          account: this.$route.params.account
        }
        this.$http({
          method: "GET",
          url: apiConfig.app.user.findPwdByAccount,
          params: params,
          emulateJSON: true
        }).then(function (resp) {
          if (resp.body.code == "200") {
            this.isValidateing = true
            var t = 60
            var countDown = function () {
              if (t > 0) {
                _this.validateMsg = "倒计时" + t-- + "s"
                setTimeout(countDown, 1000)
              } else {
                _this.validateMsg = "重新发送"
                _this.isValidateing = false
              }
            }
            countDown()

          } else {
            this.$Message.warning(resp.body.msg)
          }
        })
      }
    }
  }
</script>
